package com.jzl.swing.ui;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.jzl.swing.entity.User;
import com.jzl.swing.service.UserService;
import com.jzl.swing.service.impl.UserServiceImpl;

import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoginUI {

	private JFrame frame;
	private JPanel jPanelMain;
	private JLabel label;
	private JTextField textField;
	private JLabel label_1;
	private JPasswordField passwordField;
	private JLabel label_2;
	private JButton button;
	private UserService userService= new UserServiceImpl();
//	private JLabel jLabelName;
//	private JLabel jLabelPass;
//	private JLabel jLabelIden;
//	private JTextField jTextFieldName;
//	private JPasswordField jPasswordField;
//	
//	private JButton jButtonLogin;
//	private JButton jButtonReset;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginUI window = new LoginUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LoginUI() {
		initialize();
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 612, 398);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		jPanelMain = new JPanel();
		jPanelMain.setLayout(null);
		
//		
//		jLabelName = new JLabel("姓名");
//		jLabelPass = new JLabel("密码");
//		jLabelIden = new JLabel("身份");
//		jButtonLogin = new JButton("登录");
//		jButtonReset = new JButton("重置");
//		jTextFieldName = new JTextField(20);
//		jPasswordField = new JPasswordField(20);
//		
//		
		
		
		
		
		
		
		
		label = new JLabel("姓名");
		label.setBounds(229, 185, 32, 16);
		jPanelMain.add(label);
		
		textField = new JTextField();
		textField.setBounds(258, 180, 130, 26);
		jPanelMain.add(textField);
		textField.setColumns(10);
		
		
		
		
		label_1 = new JLabel("密码");
		label_1.setBounds(229, 223, 32, 16);
		jPanelMain.add(label_1);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(258, 213, 130, 26);
		jPanelMain.add(passwordField);
		
		
		label_2 = new JLabel("身份");
		label_2.setBounds(229, 263, 61, 16);
		jPanelMain.add(label_2);
		
		JComboBox<String> comboBox = new JComboBox<String>();
		comboBox.addItem("普通员工");
		comboBox.addItem("管理员");
		comboBox.setBounds(258, 259, 130, 27);
		jPanelMain.add(comboBox);
		
		
		JButton btnNewButton = new JButton("登录系统");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if ("".equals(textField.getText())
						|| textField.equals(null)
						|| "".equals(String.valueOf(passwordField.getPassword()))) {
					JOptionPane.showMessageDialog(jPanelMain, "用户名或密码不能为空");
					return;
				}
				String user_name = textField.getText();
				String user_pass = String.valueOf(passwordField.getPassword());
				String user_iden = comboBox.getSelectedItem().toString();
//				System.out.println(user_name+user_pass+user_iden);
				try {
					if (userService.login(new User(user_name, user_pass, user_iden))) {
						frame.setVisible(false);
						String[] args = {user_name, user_pass, user_iden};
						MainUI.main(args);
					}else {
						JOptionPane.showMessageDialog(jPanelMain, "用户名或密码或身份错误");
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		btnNewButton.setBounds(196, 312, 117, 29);
		jPanelMain.add(btnNewButton);
		
		button = new JButton("重置");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText("");
				passwordField.setText("");
				
			}
		});
		button.setBounds(325, 312, 117, 29);
		jPanelMain.add(button);
		
		addPics(jPanelMain);
		frame.getContentPane().add(jPanelMain);
		frame.setVisible(true);
	}
	
	private void addPics(JPanel jPanel) {
        ImageIcon ii = new ImageIcon("images/login_box.jpg");
        int width = ii.getIconWidth();
        int height = ii.getIconHeight();
        
        
        BackImage bi = new BackImage(width, height, "images/login_box.jpg");
        bi.setBounds(0, 0, width, height);
        jPanel.add(bi);
	}
}
class BackImage extends JPanel
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Image im;
    int width;
    int height;
    public BackImage(int width, int height, String filePath)
    {
        this.width = width;
        this.height = height;
        try
        {
            im = ImageIO.read(new File(filePath));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    public void paintComponent(Graphics g)
    {
        g.drawImage(im, 0, 0, width, height, this);
    }
    
	public void addPics(JPanel jPanel, String FilePath) {
        ImageIcon ii = new ImageIcon(FilePath);
        int width = ii.getIconWidth();
        int height = ii.getIconHeight();
        
        
        BackImage bi = new BackImage(width, height, FilePath);
        bi.setBounds(0, 0, width, height);
        jPanel.add(bi);
	}
}